/*
 * Copyright 2011 Clean GWT
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.cleangwt.validation.client.constraints;

import com.cleangwt.validation.client.BaseValidation;

/**
 * The {@link Validation} for {@link Size}.
 * 
 * @author Jess
 * @version 1.0
 * @since 1.0
 */
public class SizeValidation extends BaseValidation {
  private long min;
  private long max;

  /**
   * Creates a size validator with the given min, max, message and default group.
   * 
   * @param min the min size constraint
   * @param max the max size constraint
   * @param message the error message
   */
  public SizeValidation(long min, long max, String message) {
    this(min, max, message, (String[]) null);
  }

  /**
   * Creates a size validator with the given min, max, message and groups.
   * 
   * @param min the min size constraint
   * @param max the max size constraint
   * @param message the error message
   * @param groups the groups
   */
  public SizeValidation(long min, long max, String message, String... groups) {
    super(message, groups);
    assert min >= 0 && max >= 1 && max >= min : "Illegal Arguments";
    this.min = min;
    this.max = max;
  }

  @Override
  protected boolean isValid(Object[] values) {
    if (values[0] == null) {
      return true;
    }
    String v = ((String) values[0]);
    return v.length() >= min && v.length() <= max;
  }
}
